const { responsSuccess, responsError } = require("../utils/responseUtil");
const {
  getCodeCountListByDate,
  getCodeCountListSummary,
  softDeleteCodeCountDetail,
  getCodeCountByBranchId,
  getGroupSummaryByBranchId,
  getBranchSummaryByBranchId
} = require("../model/codeCount");
const { validateParams } = require("../utils/validateReqParam");
const Mail = require("../script/mail");
const { countRepoSpecDateSpan } = require("../script/countRepo");
const { scriptIsRunningError } = require("../commonError/index");
const { getRepoListByBranchId } = require("../model/repos");
const { output, mailConfig } = require("../config/config");
const { getQuarter } = require("../utils/DateUtil");
const { getRenderDailyTpl } = require("../script/renderDailyTpl");

module.exports.getCodeCountListByDateController = function (request, response) {
  validateParams(request.body, {
    //url参数即使传递数字，接收到也是string类型。
    startDate: { required: false, type: "string", default: null },
    endDate: { required: false, type: "string", default: null },
    totalNumAsc: { required: false, type: "boolean", default: null },
    hasZero: { required: false, type: "boolean", default: true },
    pageNum: { required: true, type: "number", default: null },
    pageSize: { required: true, type: "number", default: null },
  })
    .then((reqData) => {
      getCodeCountListByDate(reqData)
        .then((res) => {
          const { totalCount, results } = res;
          responsSuccess(response, {
            total: totalCount,
            countList: results,
          });
        })
        .catch((err) => {
          console.log("getCodeCountListByDate --> err", err);
          responsError(response, err);
        });
    })
    .catch((err) => {
      console.log("getCodeCountListByDate --> err", err);
      responsError(response, err);
    });
};
module.exports.sendMailController = async function (request, response) {
  try {
    const { specDate: nyrDate } = await validateParams(request.body, {
      //url参数即使传递数字，接收到也是string类型。
      specDate: { required: true, type: "string", default: null },
    });

    const html = await getRenderDailyTpl(1, nyrDate);

    responsSuccess(response, null);
    await Mail.send(
      `【千寻】报价研发部-代码量统计(${nyrDate})`,
      mailConfig.sendAddress,
      html
    );
  } catch (err) {
    responsError(response, err);
  }
};

module.exports.codeCountListSummaryController = async (request, response) => {
  try {
    let res = await getCodeCountListSummary(1);
    responsSuccess(response, res);
  } catch (err) {
    console.log("getCodeCountListSummary --> err", err);
    responsError(response, err);
  }
};

module.exports.softDeleteCodeCountDetailController = async (
  request,
  response
) => {
  validateParams(request.params, {
    //url参数即使传递数字，接收到也是string类型。
    rid: { required: true, type: "string", strReg: /^\d+$/, default: null },
  })
    .then(async (reqData) => {
      const { rid } = reqData;
      try {
        await softDeleteCodeCountDetail(rid);
        responsSuccess(response, null);
      } catch (err) {
        console.log("softDeleteCodeCountDetailController --> err", err);
        responsError(response, err);
      }
    })
    .catch((err) => {
      console.log("softDeleteCodeCountDetailController --> err", err);
      responsError(response, err);
    });
};

let countReposLock = false; //锁
module.exports.countReposController = async function (request, response) {
  const { startDate, endDate } = await validateParams(request.body, {
    startDate: {
      required: true,
      type: "string",
      strReg: /^\d{4}-\d{1,2}-\d{1,2}$/,
    },
    endDate: {
      required: true,
      type: "string",
      strReg: /^\d{4}-\d{1,2}-\d{1,2}$/,
    },
  });
  if (countReposLock === false) {
    countReposLock = true;
    try {
      let reposList = await getRepoListByBranchId({
        branchId: 1,
      });
      responsSuccess(response, "脚本执行中");
      await countRepoSpecDateSpan(
        reposList,
        startDate,
        endDate,
        "12:00:00",
        output
      );
    } catch (err) {
      responsError(response, err);
    }
    countReposLock = false;
    return;
  }

  responsError(response, new scriptIsRunningError());
};

module.exports.quarterController = async function (request, response) {
  responsSuccess(response, getQuarter());
};

module.exports.previewEmailController = async function (request, response) {
  try {
    const { nyrDate } = await validateParams(request.params, {
      nyrDate: {
        required: true,
        type: "string",
        strReg: /^\d{4}-\d{1,2}-\d{1,2}$/,
      },
    });
    let html = await getRenderDailyTpl(1, nyrDate);
    responsSuccess(response, html);
  } catch (err) {
    responsError(response, err, "预览数据出错");
  }
};

module.exports.getCodeCountListByBranchIdController = async function (
  request,
  response
) {
  try {
    const { branchId, startDate, endDate, pageNum, pageSize } =
      await validateParams(request.body, {
        branchId: { required: true, type: "number" },
        startDate: {
          required: true,
          type: "string",
          strReg: /^\d{4}-\d{1,2}-\d{1,2}$/,
        },
        endDate: {
          required: true,
          type: "string",
          strReg: /^\d{4}-\d{1,2}-\d{1,2}$/,
        },
        pageNum: { required: true, type: "number" },
        pageSize: { required: true, type: "number" },
      });

    const res = await getCodeCountByBranchId(
      branchId,
      startDate,
      endDate,
      pageNum,
      pageSize
    );
    responsSuccess(response, res);
  } catch (err) {
    responsError(response, err);
  }
};

module.exports.getGroupSummaryByBranchIdController = async function (
  request,
  response
) {
  try {
    const { branchId, quarter } = await validateParams(request.body, {
      branchId: { required: true, type: "number" },
      quarter: {
        required: true,
        type: "string",
      },
    });

    const res = await getGroupSummaryByBranchId(branchId, quarter);
    responsSuccess(response, res);
  } catch (err) {
    responsError(response, err);
  }
};


module.exports.getBranchSummaryController = async function (
    request,
    response
) {
  try {
    const { branchId, startNyrDate,endNyrDate } = await validateParams(request.body, {
      branchId: { required: true, type: "number" },
      startNyrDate:{
        required: true,
        type: "string",
      },
      endNyrDate: {
        required: true,
        type: "string",
      },
    });

    const res = await getBranchSummaryByBranchId(branchId,
        startNyrDate,
        endNyrDate);
    responsSuccess(response, res);
  } catch (err) {
    responsError(response, err);
  }
};
